<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Javascript (View Helper)
 *
 * @package    helper
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_View_Helper_Javascript extends EGP_View_Helper_Abstract
{

    /**
     * 运行 Javascript 脚本
     *
     * @param  string  $script  Javascript 脚本文本
     * @param  boolean $return  是否返回脚本
     * @return string|void
     */
    public function init($script = '')
    {
        @header("Content-type: text/html; charset=utf-8");
        if (!empty($script)) {
            $script = "<script language=\"javascript\" type=\"text/javascript\">\n" .
                      "$script\n</script>\n";
            echo $script;
        }
        return $this;
    }

    /**
     * 弹出 Javascript 信息提示窗口
     *
     * @param  string   $message  提示信息
     */
    public function alert($message = '') {
        $this->init("alert(\"$message\")");
    }

    /**
     * 调用 Javascript 信息确认窗口，且根据结果不同前往指定的URL地址
     *
     * @param  string  $message  提示信息
     * @param  string  $url1     判断为 true 时前往地址
     * @param  string  $url2     判断为 false 时前往地址
     * @param  string  $target   目标框架
     */
    public function confirm($message, $url1, $url2 = '', $target = 'self') {
        $script = "var url1 = '$url1';\nvar url2 = '$url2';\n" .
                  "confirm('$message') ?\n" .
                  "\t(url1 == 'back' ? history.back(-1) : $target.location = url1) :\n" .
                  "\t(url2 == 'back' ? history.back(-1) : $target.location = url2);";
        $this->init($script);
    }

    /**
     * 前往指定的地址
     *
     * @param  string  $url      前往的URL地址
     * @param  string  $message  需要弹出的提示信息
     * @param  string  $sub      目标框架
     */
    public function goto($url, $message = '', $sub = '') {
        $script = '';
        !empty($message) && $script .= "alert(\"$message\");";
        $script .= empty($sub) ? "window.location=\"$url\";" : "window.$sub.location=\"$url\";";
        $this->init($script);
        die();
    }

    /**
     * 调用 JS 返回上一页
     *
     * @param  string  $message  提示信息
     */
    public function back($message = '') {
        $script = '';
        !empty($message) && $script .= "alert(\"$message\");";
        $script .= 'history.back();';
        $this->init($script);
        die();
    }

    /**
     * 关闭窗口
     *
     * @param  string  $message  提示信息
     * @param  string  $refresh  需要重新刷新的目标框架
     */
    public function close($message = '', $refresh = '') {
        $script = '';
        !empty($msg) && $script .= "window.alert(\"$message\");";
        !empty($refresh) && $script .= "$refresh.reload(true);";
        $this->init($script . "window.close();");
        die();
    }

}